#ifdef ENABLE_NETWORK


#include "user.h"
#include "../menu.h"
#include "chipsupport.h"
#include "AVReg.h"
#include "aw_config.h"
#include "i2c_communication.h"

#define NET_ENABLE_ADDR             (JPEG_PLAY_MODE_ADDR + 1+5) 

#define NETIF_CONFIG_ADDR           (NET_ENABLE_ADDR+1)          
#define WIFI_CONFIG_ADDR            (NETIF_CONFIG_ADDR + sizeof(struct aml_netif_setting))	
#define YAHOO_AUTO_ENABLE_ADDR     (WIFI_CONFIG_ADDR+sizeof(struct aml_iw_config))
#define GOOGLE_AUTO_ENABLE_ADDR	(YAHOO_AUTO_ENABLE_ADDR + 1)
#define YAHOO_CONFIG_ADDR   (GOOGLE_AUTO_ENABLE_ADDR + 1)
#define GOOGLE_CONFIG_ADDR  (YAHOO_CONFIG_ADDR + sizeof(yahoo_login_info_t))

#include "aw_network.h"


struct aml_iw_config     aml_wireless_config;
struct aml_netif_setting aml_network_config;
u8_t yahoo_auto_enable;
u8_t google_auto_enable;
yahoo_login_info_t yahoo_config;
google_login_info_t google_config;

int GetAmlWifiConfig(struct kodak_iw_config *config);
int SetAmlWifiConfig(struct kodak_iw_config *config);
int GetAmlWifiConfigCache(struct iw_config *config);
//int SetKodakWifiConfigCache(struct iw_config *config);

int GetAmlNetConfig(struct kodak_netif_setting *config);
int SetAmlNetConfig(struct kodak_netif_setting *config);
int GetAmlNetConfigCache(struct netif_setting *config);
int SetAmlNetConfigCache(struct netif_setting *config);
#ifdef PARA_IN_NAND
int GetWifiConfig(struct aml_iw_config *config)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,WIFI_CONFIG_ADDR,SEEK_SET);	
	read(nand_fd, (struct aml_iw_config*)config, sizeof(aml_iw_config)) ;
	close(nand_fd);
	return TRUE;
}

int SetWifiConfig(struct aml_iw_config *config)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,WIFI_CONFIG_ADDR,SEEK_SET);	
    write(nand_fd, (struct aml_iw_config*)config, sizeof(aml_iw_config)) ;
	close(nand_fd);
	return TRUE;
	
}


int GetNetConfig(struct aml_netif_setting *config)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,NETIF_CONFIG_ADDR,SEEK_SET);	
	read(nand_fd, (struct aml_netif_setting*)config, sizeof(aml_netif_setting)) ;
	close(nand_fd);
	return TRUE;    
}

int SetNetConfig(struct aml_netif_setting *config)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,NETIF_CONFIG_ADDR,SEEK_SET);	
    write(nand_fd, (struct aml_netif_setting*)config, sizeof(aml_netif_setting)) ;
	close(nand_fd);
	return TRUE;
}

unsigned char GetYahooAutoEnable(char *enable)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,YAHOO_AUTO_ENABLE_ADDR,SEEK_SET);	
	read(nand_fd, (unsigned char *)enable, 1) ;
	close(nand_fd);
	return TRUE;   
}

unsigned char SetYahooAutoEnable(char * enable)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,YAHOO_AUTO_ENABLE_ADDR,SEEK_SET);	
	write(nand_fd, (unsigned char *)enable, 1) ;
	close(nand_fd);
	return TRUE;
}


unsigned char GetGoogleAutoEnable(char * enable)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,GOOGLE_AUTO_ENABLE_ADDR,SEEK_SET);	
	read(nand_fd, (unsigned char *)enable, 1) ;
	close(nand_fd);
	return TRUE;   
}

unsigned char SetGoogleAutoEnable(char * enable)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,GOOGLE_AUTO_ENABLE_ADDR,SEEK_SET);	
	write(nand_fd, (unsigned char *)enable, 1) ;
	close(nand_fd);
	return TRUE;  
}

unsigned char GetYahooConfig(yahoo_login_info_t *login)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,YAHOO_CONFIG_ADDR,SEEK_SET);	
	read(nand_fd, (struct yahoo_login_info_t*)login, sizeof(yahoo_login_info_t)) ;
	close(nand_fd);
	return TRUE;   
}

unsigned char SetYahooConfig(yahoo_login_info_t *login)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,YAHOO_CONFIG_ADDR,SEEK_SET);	
    write(nand_fd, (struct yahoo_login_info_t*)login, sizeof(yahoo_login_info_t)) ;
	close(nand_fd);
	return TRUE;
}

unsigned char GetGoogleConfig(google_login_info_t *login)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,GOOGLE_CONFIG_ADDR,SEEK_SET);	
	read(nand_fd, (struct google_login_info_t*)login, sizeof(google_login_info_t)) ;
	close(nand_fd);
	return TRUE;   
}

unsigned char SetGoogleConfig(google_login_info_t *login)
{
    int  nand_fd;

	nand_fd = open("/dev/nandsys",O_RDWR);
	if(nand_fd<0)
		return FALSE;
	lseek(nand_fd,GOOGLE_CONFIG_ADDR,SEEK_SET);	
	write(nand_fd, (struct google_login_info_t*)login, sizeof(google_login_info_t)) ;
	close(nand_fd);
	return TRUE;
}

#else

//#define EEPROM_ENABLE

int GetWifiConfig(struct aml_iw_config *config)
{
#ifdef EEPROM_ENABLE
        return i2c_read_eeprom((unsigned char*)config, sizeof(*config), WIFI_CONFIG_ADDR);

#else
	strcpy(aml_wireless_config.essid,"wmr");
	aml_wireless_config.security = 0;
	aml_wireless_config.mode = IW_MODE_INFRA;
	return TRUE;
#endif
}

int SetWifiConfig(struct aml_iw_config *config)
{
#ifdef EEPROM_ENABLE
        return i2c_write_eeprom((unsigned char*)config, sizeof(*config), WIFI_CONFIG_ADDR);
#else
	return TRUE;
#endif
}


int GetNetConfig(struct aml_netif_setting *config)
{
#ifdef EEPROM_ENABLE
        return i2c_read_eeprom((unsigned char*)config, sizeof(*config), NETIF_CONFIG_ADDR);
#else
	aml_network_config.dhcp = 1;
	aml_network_config.status = 1;
	return TRUE;
#endif
}

int SetNetConfig(struct aml_netif_setting *config)
{
#ifdef EEPROM_ENABLE
	if (config != &aml_network_config)
		memcpy(&aml_network_config, config, sizeof(aml_network_config));
		return i2c_write_eeprom((unsigned char*)config, sizeof(*config), NETIF_CONFIG_ADDR);
#else
	return TRUE;
#endif
}

unsigned char GetYahooAutoEnable(char *enable)
{
#ifdef EEPROM_ENABLE
          return i2c_read_eeprom((unsigned char*)enable, sizeof(*enable), YAHOO_AUTO_ENABLE_ADDR);
#else
	yahoo_auto_enable = 0;
	return TRUE;
#endif
}

unsigned char GetGoogleAutoEnable(char * enable)
{
#ifdef EEPROM_ENABLE
          return i2c_read_eeprom((unsigned char*)enable, sizeof(*enable), GOOGLE_AUTO_ENABLE_ADDR);
#else
	google_auto_enable = 0;
	return TRUE;
#endif
}


unsigned char GetYahooConfig(yahoo_login_info_t *login)
{
#ifdef EEPROM_ENABLE
          return i2c_read_eeprom((unsigned char*)login, sizeof(*login), YAHOO_CONFIG_ADDR);
#else
	strcpy(yahoo_config.username,"avos.amlogic@yahoo.com");
	strcpy(yahoo_config.passphrase,"");
	return TRUE;
#endif
}

unsigned char GetGoogleConfig(google_login_info_t *login)
{
#ifdef EEPROM_ENABLE
          return i2c_read_eeprom((unsigned char*)login, sizeof(*login), GOOGLE_CONFIG_ADDR);
#else
	strcpy(google_config.username,"avostest");
	strcpy(google_config.passphrase,"avos88");
	return TRUE;	
#endif
}

unsigned char SetYahooConfig(yahoo_login_info_t *login)
{
#ifdef EEPROM_ENABLE
          return i2c_write_eeprom((unsigned char*)login, sizeof(*login), YAHOO_CONFIG_ADDR);
#else
	return TRUE;
#endif
}

unsigned char SetGoogleConfig(google_login_info_t *login)
{
#ifdef EEPROM_ENABLE
          return i2c_write_eeprom((unsigned char*)login, sizeof(*login), GOOGLE_CONFIG_ADDR);
#else
	return TRUE;
#endif
}

unsigned char SetYahooAutoEnable(char * enable)
{
#ifdef EEPROM_ENABLE       
          return i2c_write_eeprom((unsigned char*)enable, sizeof(*enable), YAHOO_AUTO_ENABLE_ADDR);
#else
	return TRUE;
#endif
}

unsigned char SetGoogleAutoEnable(char * enable)
{
#ifdef EEPROM_ENABLE
          return i2c_write_eeprom((unsigned char*)enable, sizeof(*enable), GOOGLE_AUTO_ENABLE_ADDR);

#else
	return TRUE;
#endif
}
#endif

#endif /* ENABLE_NETWORK */
